package com.huang.datebase;

import com.huang.info.UserInfo;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class UserLog {
	//根据手机号查询用户信息
	public UserInfo queryByPhoneNum(String phoneNum) {

		Connection connection =null;
		PreparedStatement ps = null;
		ResultSet resultSet = null;
		UserInfo userInfo = new UserInfo();

		try {
			connection = DbUtil.getConnection();

			String sql = " SELECT * FROM user_info WHERE phone = ? ";

			ps = connection.prepareStatement(sql);
			ps.setString(1, phoneNum);

			resultSet = ps.executeQuery();

			while (resultSet.next()) {

				String phone = resultSet.getString("phone");
				String password = resultSet.getString("password");

				userInfo.setPhone(phone);
				userInfo.setPassword(password);

			}

		} catch (SQLException throwables) {
			throwables.printStackTrace();
		}finally {
			DbUtil.close(connection, null, ps, resultSet);
			return userInfo;
		}

	}

	public List<UserInfo> queryAllUserInfo() throws SQLException {

		ArrayList<UserInfo> userInfoList = new ArrayList<UserInfo>();

		Connection connection = null;

		Statement statement = null;

		ResultSet resultSet = null;


			String sql = "select * from user_info";

			connection = DbUtil.getConnection();

			statement = connection.createStatement();

			resultSet = statement.executeQuery(sql);

			while (resultSet.next()) {

				int id = resultSet.getInt("id");
				String phone = resultSet.getString("phone");
				String password = resultSet.getString("password");
				String sex = resultSet.getString("sex");
				String hobby = resultSet.getString("hobby");
				int age = resultSet.getInt("age");
				String header = resultSet.getString("header");
				String education = resultSet.getString("education");
				String myself = resultSet.getString("myself");

				UserInfo userInfo = new UserInfo();

				userInfo.setId(id+"");
				userInfo.setPhone(phone);
				userInfo.setPassword(password);
				userInfo.setSex(sex);
				userInfo.setHobby(hobby);
				userInfo.setAge(age+"");
				userInfo.setHeader(header);
				userInfo.setEducation(education);
				userInfo.setMyself(myself);

				userInfoList.add(userInfo);
			}

			DbUtil.close(connection, statement,null,resultSet);


		return userInfoList;

	}
}
